package com.spic.business.plan.team.service.impl;

import java.util.List;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spic.business.plan.team.mapper.PlanGroupMapper;
import com.spic.business.plan.team.domain.PlanGroup;
import com.spic.business.plan.team.service.IPlanGroupService;

/**
 * 分组Service业务层处理
 * 
 * @author spic
 * @date 2021-11-10
 */
@Service
public class PlanGroupServiceImpl implements IPlanGroupService 
{
    @Autowired
    private PlanGroupMapper planGroupMapper;

    /**
     * 查询分组
     * 
     * @param id 分组主键
     * @return 分组
     */
    @Override
    public PlanGroup selectPlanGroupById(Long id)
    {
        return planGroupMapper.selectPlanGroupById(id);
    }

    /**
     * 查询分组列表
     * 
     * @param planGroup 分组
     * @return 分组
     */
    @Override
    public List<PlanGroup> selectPlanGroupList(PlanGroup planGroup)
    {
        return planGroupMapper.selectPlanGroupList(planGroup);
    }

    /**
     * 新增分组
     * 
     * @param planGroup 分组
     * @return 结果
     */
    @Override
    public int insertPlanGroup(PlanGroup planGroup)
    {
        planGroup.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        planGroup.setCreateTime(DateUtils.getNowDate());
        return planGroupMapper.insertPlanGroup(planGroup);
    }

    /**
     * 修改分组
     * 
     * @param planGroup 分组
     * @return 结果
     */
    @Override
    public int updatePlanGroup(PlanGroup planGroup)
    {
        return planGroupMapper.updatePlanGroup(planGroup);
    }

    /**
     * 批量删除分组
     * 
     * @param ids 需要删除的分组主键
     * @return 结果
     */
    @Override
    public int deletePlanGroupByIds(Long[] ids)
    {
        return planGroupMapper.deletePlanGroupByIds(ids);
    }

    /**
     * 删除分组信息
     * 
     * @param id 分组主键
     * @return 结果
     */
    @Override
    public int deletePlanGroupById(Long id)
    {
        return planGroupMapper.deletePlanGroupById(id);
    }
    /**
     * 校验队伍分组名称是否重复
     *
     * @param planGroup 应急队伍分组
     * @return 结果 0 不重复  1重复  2修改不重复 新增重复
     */
    public int checkName(PlanGroup planGroup){
        if(StringUtils.isNotEmpty(planGroup.getGroupName())) {
            if (planGroupMapper.selectByName(planGroup.getGroupName())!=null){
                if (!planGroupMapper.selectByName(planGroup.getGroupName()).getId().equals(planGroup.getId())){
                    return 1;
                }else{
                    return 2;
                }
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }
}
